<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * film CRUD model class 
 */
Class Film_model extends CI_Model {

    public $film_table;
    public $film_mufaj_table;
    public $events_table;
    public $programsav_table;

    function __construct() {
        // Call the Model constructor
        parent::__construct();
        // film műfaj model betöltése
        $this->load->model('film_mufaj_model');
        // táblázat belabelezése
        $this->film_mufaj_table = $this->db->dbprefix('film_mufajok');
        $this->film_table = $this->db->dbprefix('filmek');
        $this->events_table = $this->db->dbprefix('esemeny');
        $this->programsav_table = $this->db->dbprefix('programsav');
    }

    /**
     * 
     * Add new film
     * 
     * @param type $film_data
     */
    function add_film($film_data = array()) {
        // insert film_data
        $this->db->insert($this->film_table, $film_data);
    }

    /**
     * 
     * 
     * 
     * Update film by id.
     * 
     * 
     * @return true on success
     */
    function update_film($film_data = array()) {
        // if film does not exists throw film_not_exists_exception
        if ($this->get_film($film_data['film_id']) == FALSE)
            throw new not_exists_exception("film");

        // do selection and update data
        $this->db->where('film_id', $film_data['film_id']);
        $this->db->update($this->film_table, $film_data);
    }

    /**
     * 
     * 
     * 
     * Delete film
     * 
     * 
     * 
     * @param type $id
     */
    function delete($film_id) {
        // delete film data from film_table
        $this->db->where('film_id', $film_id);
        $this->db->delete($this->film_table);
    }

    /**
     * 
     * returns the film (for pagination)
     * 
     * options:
     *  select 
     *  search
     *  order_by
     *  order_dir
     *  limit
     *  offset
     * 
     * @param type $limit the number of returned film
     * @param type $offset from this number.
     * @return type 
     */
    function get_films($options = array()) {
        if (isset($options['select'])) {
            $this->db->select($options['select']);
        }

        $this->db->join($this->film_mufaj_table, "$this->film_table.film_mufaj_id = $this->film_mufaj_table.mufaj_id", 'left');

        if (isset($options['limit']) && isset($options['offset']))
            $this->db->limit($options['limit'], $options['offset']);

        if (isset($options['order_by'])) {
            if (isset($options['order_dir'])) {
                $this->db->order_by($options['order_by'], $options['order_dir']);
            } else {
                $this->db->order_by($options['order_by']);
            }
        }

        // have to be an array
        if (isset($options['where'])) {
            $this->db->where($options['where']);
        }

        if (isset($options['search_like'])) {
            $this->db->like('eredeti_cim', $options['search_like']);
            $this->db->or_like('magyar_cim', $options['search_like']);
            $this->db->or_like('rendezo', $options['search_like']);
            $this->db->or_like('hossz', $options['search_like']);
            $this->db->or_like('foszereplok', $options['search_like']);
            $this->db->or_like('mufaj_nev', $options['search_like']);
        }

        $query = $this->db->get($this->film_table);

        //echo $this->db->last_query();

        return $query->result();
    }

    /**
     * 
     * Returns the number of film
     * 
     * 
     * @return type 
     */
    function count() {
        return $this->db->count_all_results($this->film_table);
    }

    /**
     * Gives back film by id 
     * 
     * @param type $id
     * @return array (film_id, filmname, film)
     */
    function get_film($id) {
        // constructing active record query
        $query = $this->db->get_where($this->film_table, array('film_id' => $id));
        // do the query
        $result = $query->result();
        if ($result == NULL)
            return NULL;
        return $result[0];
    }

    /**
     * Gives back film by id 
     * 
     * @param type $id
     * @return array (film_id, filmname, film)
     */
    function get_film_by_slug($slug, $ignore_id = NULL) {
        // constructing active record query
        // ignore id
        if ($ignore_id) {
            $this->db->where_not_in('film_id', $ignore_id);
        }
        $query = $this->db->get_where($this->film_table, array('eredeti_cim_slug' => $slug));
        // do the query
        $result = $query->result();
        if ($result == NULL)
            return NULL;
        return $result[0];
    }

    /**
     * 
     * visszaadja az eseményeket a range-ben
     * 
     */
    function get_in_range($start, $end) {
        $this->db->select('esemeny_cim, esemeny_cim_slug, esemeny_kezdes, esemeny_vege, eredeti_cim, magyar_cim, eredeti_cim_slug');

        $this->db->where('esemeny_kezdes >', $start);
        $this->db->or_where('esemeny_vege <', $end);

        // csak azok az események kellenek, amikhez van film: right join
        $this->db->join($this->film_table, "$this->events_table.esemeny_film_id = $this->film_table.film_id", 'right');

        $query = $this->db->get($this->events_table);
        $result = $query->result();

        return $result;
    }

}